package com.google.coffeeshop.gui.desktop.orderpayment;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.business.MaintenanceBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.SwingFactory;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class StopSeatActionsPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private SeatDTO seat = null;
	private StopSeatActionsFrame parent = null;
	
	public StopSeatActionsPanel(StopSeatActionsFrame parent, SeatDTO seat) {
		this.seat = seat;
		this.parent = parent;
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblMessage = new JLabel("This Seat is maintaining... You have two choices to do:");
		add(lblMessage, "2, 2, 3, 1");
		
		JButton btnReuse = new JButton("Reuse This");
		btnReuse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				reuseSeat();
			}
		});
		btnReuse.setIcon(new ImageIcon(StopSeatActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/check-24.png")));
		add(btnReuse, "2, 4");
		
		JButton btnTerminate = new JButton("Terminate This");
		btnTerminate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				boolean ok = SwingFactory.showConfirmDialog("Are you sure to delete the seat?");
				if (ok) {
					terminateSeat();
				}
			}
		});
		btnTerminate.setIcon(new ImageIcon(StopSeatActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/stop-24.png")));
		add(btnTerminate, "4, 4");
	}

	private void reuseSeat() {
		if (seat != null) {
			if (!seat.getStatus().equals(GeneralConstants.SEAT_STOP_STATUS)) {
				SwingFactory.showMessageDiaglogInform("Seat have to be STOP status to reuse it.");
				return;
			}
			MaintenanceBusiness.reuseSeat(seat.getId());
			parent.disposeFrame();
			parent.refreshParentGUI();
		}
	}
	
	private void terminateSeat() {
		if (seat != null) {
			if (!seat.getStatus().equals(GeneralConstants.SEAT_STOP_STATUS)) {
				SwingFactory.showMessageDiaglogInform("Seat have to be STOP status to terminate it.");
				return;
			}
			MaintenanceBusiness.destroySeat(seat.getId());
			parent.disposeFrame();
			parent.refreshParentGUI();
		}
	}
	
}
